<?php
namespace App\Repository;

use App\Model\User;

use App\Facade\Event;
use App\Event\User\CreateEvent;
use App\Event\User\LockEvent;
use App\Event\User\MoneyEvent;
use App\Event\User\MoneyAllEvent;
use App\Event\User\OrderMoneyRemainEvent;
use App\Event\User\OrderMoneyRemainUserEvent;

class UserRepository extends \HyperfStore\Repository
{
    // 放入模型
    public function build()
    {
        return new User;
    }

    // 登陆
    public function login($data)
    {
        if($user = User::tokenLogin($data))
        {
            // 锁定状态
            if($user->lock_status == User::LOCK_STATUS_OK)
            {
                return;
            }
            
            // 只允许一个 token
            $user->tokenFlush();
            
            return ['token' => $user->tokenCreate('user-token')];
        }
    }
    
    // 创建
    public function create($data, UserRepository $user)
    {
        $this->model->user()->associate($user->model());
        
        // 生成关系链
        $data['user_all'] = array_merge($user->user_all, [$user->id]);
        
        $res = $this->exception(function() use($data) {
            return $this->save($data);
        });
        
        if($res)
        {
            Event::dispatch(new CreateEvent($this));
        }
        
        return $res;
    }
    
    // 更新用户
    public function update($data)
    {
        // 是否重置密码
        if(isset($data['password']) && !$data['password'])
        {
            unset($data['password']);
        }

        // 更新资料
        $res = $this->exception(function() use($data) {
            return $this->save($data);
        });
        
        if($res)
        {
            // 判断是否锁定用户
            if($data['lock_status'])
            {
                $this->setLockStatus();
            }
        }
        
        return $res;
    }
    
    // 升级
    public function setLevel(LevelRepository $level)
    {
        if($this->model->level_id < $level->id)
        {
            $this->model->level()->associate($level->model());
            
            return $this->model->save();
        }
    }
    
    // 使用
    public function makeMoney($money_type, $money, $content, $type = '')
    {
        if($this->hasMoney($money_type, $money))
        {
            return $this->addMoney($money_type, -1 * $money, $content, $type);
        }
    }
    
    // 判断
    public function hasMoney($money_type, $money)
    {
        if(!(float) $money)
        {
            return true;
        }
        
        if($money > 0)
        {
            return $this->model->$money_type >= $money;
        }
    }
    
    // 增加
    public function addMoney($money_type, $money, $content, $type = '')
    {
        if(!(float) $money)
        {
            return true;
        }
        
        $res = $this->model->increment($money_type, $money);
        
        Event::dispatch(new MoneyEvent($this, $money_type, $money, $content, $type));
            
        return $res;
    }
    
    // 累计业绩
    public function addMoneyAll($money)
    {
        $this->model->increment('money_all', $money);
        
        Event::dispatch(new MoneyAllEvent($this, $money));
        
        return true;
    }
    
    // 累加团队业绩
    public function addMoneyUser($money)
    {
        User::whereIn('id', $this->model->user_all)->increment('money_user', $money);
    }
    
    // 记录变动时间
    public function setMoneyWalletTime()
    {
        $this->model->money_wallet_time = time();
        
        return $this->model->save();
    }
    
    // 锁定用户
    public function setLockStatus()
    {
        $this->model->lock_status = User::LOCK_STATUS_OK;
        
        Event::dispatch(new LockEvent($this));
        
        return $this->model->save();
    }
    
    // 静态关闭
    public function setMoneyStatus()
    {
        $this->model->money_status = User::MONEY_STATUS_OK;
        
        return $this->model->save();
    }
    
    // 静态关闭
    public function moneyStatus()
    {
        return $this->model->money_status == User::MONEY_STATUS_OK;
    }
    
    // 判断今日可释放静态
    public function getMoneyRemainTime()
    {
        // 今日时间
        $time = strtotime(date("Y-m-d"), time());
        
        return $this->model->money_remain_time < $time;
        
    }
    
    // 更新静态释放时间
    public function setMoneyRemainTime($time = null)
    {
        $this->model->money_remain_time = $time ?? time();
        
        return $this->model->save();
    }
    
    // 动态释放
    public function orderMoneyRemain($type, $money, $content)
    {
        Event::dispatch(new OrderMoneyRemainEvent($this, $type, $money, $content));
        
        // 更新动态时间
        $this->model->money_time = time();
        
        return $this->model->save();
    }
    
    // 积分奖励分入帐户
    public function orderMoneyRemainUser($type, $money, $content)
    {
        Event::dispatch(new OrderMoneyRemainUserEvent($this, $type, $money, $content));
    }
}